<?php
$op = get_get("op");
if ($op == "") {
    $op = "requestPage";
}
if ($op == "requestPage") {
    $page = get_get("page");
    if (! $page)
        $page = "add_user";
    if ($page == "add_user") {
        loadTemp("admin/models/member/add_user.php");
        die();
    }
    if ($page == "edit_user") {
        loadTemp("admin/models/member/edit_user.php");
        die();
    }
    if ($page == "ban_user") {
        loadTemp("admin/models/member/ban_user.php");
        die();
    }
    if ($page == "parent_user") {
        loadTemp("admin/models/member/parent_user.php");
        die();
    }
}
if ($op == "add_user") {
    $passwd = get_post("passwd");
    if (strlen($passwd) < 6) {
        returnMessage(- 1, "密码长度应当大于6位");
    }
    $phone = get_post("phonenumber");
    if (! ($phone == "" || isPhoneNumber($phone))) {
        returnMessage(- 2, "手机号格式不合法");
    }
    $email = get_post("mail");
    if (! ($email == "" || isEmail($email))) {
        returnMessage(- 3, "电子邮件格式格式不合法");
    }
    if ($email == "" && $phone == "") {
        returnMessage(- 4, "手机号或电子邮件必选一个");
    }
    $backcode = register_login_info($passwd, $email, $phone);
    if ($backcode == 1) {
        returnMessage(1, "邮箱已经被注册过了");
    }
    if ($backcode == 2) {
        returnMessage(2, "手机已经被注册过了");
    }
    global $_G;
    $uid = $_G["admincache"]["registerUID"];
    die(returnMessage(0, "注册成功，新注册用户UID为：$uid"));
}
if ($op == "search_edit_user") {
    $account = get_post("account");
    if (! $account)
        returnMessage(- 1, "你未填写账号");
    $uid = getUID($account);
    if ($uid == - 1)
        returnMessage(- 2, "账号不存在");
    $back = array();
    $isNew = isNew($uid);
    $back["isNew"] = $isNew;
    if (! $isNew)
        $back["profile"] = getProfile($uid);
    $back["logindata"] = getAllLoginData($uid);
    $back["op"] = "user_search";
    $back["uid"] = $uid;
    returnMessage(0, "操作成功", $back);
}
if ($op == "edit_user_login_data") {
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 2, "尚未选择uid");
    $passwd = get_post("passwd");
    if ($passwd != "keep01") {
        if (strlen($passwd) < 6) {
            returnMessage(- 1, "密码长度应当大于6位");
        }
        chengePasswd($uid, $passwd);
    }
    $phone = get_post("phonenumber");
    if ($phone == - 1)
        $phone = "";
    if (! ($phone == "" || isPhoneNumber($phone))) {
        returnMessage(- 2, "手机号格式不合法");
    }
    $email = get_post("mail");
    if (! ($email == "" || isEmail($email))) {
        returnMessage(- 3, "电子邮件格式格式不合法");
    }
    if ($email == "" && $phone == "") {
        returnMessage(- 4, "手机号或电子邮件必选一个");
    }
    updateLoginData($uid, $email, $phone);
    returnMessage(0, "该用户的登录信息已经修改成功，存档已经保存", array(
        "op" => "edit_done_login"
    ));
}
if ($op == "edit_user_profile") {
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 2, "尚未选择uid");
    $name = get_post("name");
    if ($name == "")
        returnMessage(- 1, "姓名不能为空");

    $gender = get_post("gender");
    if (! ($gender == 0 || $gender == 1))
        returnMessage(- 2, "性别不合法");

    $user_type = get_post("user_type");
    if (! ($user_type == 0 || $user_type == 1 || $user_type == 2))
        returnMessage(- 3, "用户身份不合法");
    $birthday = get_post("birthday");
    if ($birthday == "")
        returnMessage(- 6, "生日不能为空");
    $introduce = get_post("introduce");
    if ($introduce == "")
        returnMessage(- 4, "个人介绍不能为空");

    $motto = get_post("motto");
    if ($motto == "")
        returnMessage(- 5, "座右铭不能为空");
    global $_G;
    if (! isset($_G["profile"])) {
        createProfile($uid, $name, $user_type, $introduce, $birthday, $gender, $motto);
        returnMessage(0, "这是该用户第一次个人档案创建，目前申请已完成，存档已经保存", array(
            "op" => "edit_done_profile"
        ));
    } else {
        updateProfile($uid, $name, $user_type, $introduce, $birthday, $gender, $motto);
        returnMessage(0, "个人信息更改成功，存档已经保存", array(
            "op" => "edit_done_profile"
        ));
    }
}
if ($op == "search_ban_user") {
    $account = get_post("account");
    if (! $account)
        returnMessage(- 1, "你未填写账号");
    $uid = getUID($account);
    if ($uid == - 1)
        returnMessage(- 2, "账号不存在");
    $back = array();
    $back["bandata"] = getBandata($uid);
    $back["op"] = "user_search";
    $back["uid"] = $uid;
    returnMessage(0, "操作成功", $back);
}
if ($op == "ban_user") {
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 2, "尚未选择uid");
    $isbanned = get_post("isBanned");
    if ($isbanned == 0) {
        updateBanState($uid, false);
        returnMessage(0, "目标账号解封成功", array(
            "op" => "reset"
        ));
    } else {
        $banTime = get_post("bantime");
        if (! $banTime)
            returnMessage(- 4, "你必须填封禁时间：");

        $time = strtotime($banTime);
        $currtime = time();
        if ($time <= $currtime)
            returnMessage(- 5, "你必须选择一个比当前时间靠后的时间来封禁：");
        updateBanState($uid, true, $banTime);
        returnMessage(0, "目标账号封禁成功，截止日期：$banTime", array(
            "op" => "reset"
        ));
    }
}
if ($op == "search_parent_user") {
    $account = get_post("account");
    if (! $account)
        returnMessage(- 1, "你未填写账号");
    $uid = getUID($account);
    if ($uid == - 1)
        returnMessage(- 2, "账号不存在");
    $profile = getProfile($uid);
    if ($profile["user_type"] == 0) {
        returnMessage(- 3, "这人是学生");
        die();
    }
    $relationships = getChildren($uid);
    $relations = array();
    if ($relationships) {
        foreach ($relationships as $v) {
            $cardinfo = getCardInfo($v["childid"]);
            $relations[$v["childid"]]["uid"] = $v["childid"];
            $relations[$v["childid"]]["relationship"] = $v["relationship"];
            $relations[$v["childid"]]["data"] = $cardinfo;
        }
    }
    $back = array();
    $back["children"] = $relations;
    $back["uid"] = $uid;
    $back["op"] = "user_search";
    returnMessage(0, "操作成功", $back);
}
if ($op = "remove_children") {
    $uid = get_post("uid");
    if (! $uid)
        returnMessage(- 2, "尚未选择uid");
    $childid = get_post("childid");
    if (! $childid)
        returnMessage(- 2, "尚未选择childid");
    removeShip($uid, $childid);
    returnMessage(- 1, "你成功地拆散了这对亲人", array(
        "op" => "remove",
        "uid" => $childid
    ));
}
returnMessage(- 2, "e3t");

?>